package io.renren.modules.seller.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.exception.RRException;
import io.renren.common.utils.Constant;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.seller.dao.SellerDao;
import io.renren.modules.seller.dao.SellerTemporaryDao;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.SellerEntity;
import io.renren.modules.seller.entity.SellerTemporaryEntity;
import io.renren.modules.seller.service.SellerTemporaryService;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class SellerTemporaryServiceImpl extends ServiceImpl<SellerTemporaryDao, SellerTemporaryEntity> implements SellerTemporaryService {

    @Autowired
    private SellerDao sellerDao;
    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SellerTemporaryDao sellerTemporaryDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SellerTemporaryEntity> page = this.page(
                new Query<SellerTemporaryEntity>().getPage(params),
                new QueryWrapper<SellerTemporaryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageResult findPage(String page, String rows) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", rows);
        IPage<SellerTemporaryEntity> iPage = this.page(
                new Query<SellerTemporaryEntity>().getPage(params),
                new QueryWrapper<SellerTemporaryEntity>()
        );
        return new PageResult(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public PageResult findPage(SellerTemporaryEntity seller, String page, String rows) {
        Map<String, Object> params = new HashMap<>(8);
        params.put("page", page);
        params.put("limit", rows);
        String name = seller.getName();
        String nickName = seller.getNickName();
        IPage<SellerTemporaryEntity> iPage = this.page(
                new Query<SellerTemporaryEntity>().getPage(params),
                new QueryWrapper<SellerTemporaryEntity>()
                        .eq(StringUtils.isNotBlank(name), "name", name)
                        .eq(StringUtils.isNotBlank(nickName), "nick_name", nickName)
        );
        return new PageResult(iPage.getTotal(), iPage.getRecords());
    }

    @Override
    public void updateStatus(String sellerId, String status) {
        //2审核通过 3审核未通过
        if (Constant.SellerStatus.three.value().equals(status)) {
            SellerTemporaryEntity entity = new SellerTemporaryEntity();
            entity.setSellerId(sellerId);
            entity.setStatus(status);
            this.updateById(entity);
        } else if (Constant.SellerStatus.two.value().equals(status)) {
            SellerTemporaryEntity byId = this.getById(sellerId);
            // 保存到商家表 TODO 第一次没有 通过一次之后才有
            SellerEntity sellerEntity = sellerDao.selectById(sellerId);
            if (sellerEntity == null) {
                // TODO id error
                sellerDao.insertSelective(byId);
            } else {
                SellerEntity entity = new SellerEntity();
                BeanUtils.copyProperties(byId, entity);
                entity.setStatus(Constant.SellerStatus.two.value());
                sellerDao.updateById(entity);
            }
            // 通过-删除
            this.removeById(sellerId);
        } else {
            throw new RRException("错误的状态码");
        }
    }

    @Override
    public void settle(SysUserEntity sysUser, SellerTemporaryEntity seller) {
        int insert = sysUserDao.insert(sysUser);
        Long userId = sysUser.getUserId();
        seller.setSellerId(userId.toString());

        // TODO id 报错
        sellerTemporaryDao.insertSelective(seller);
    }

}
